<?php
/**
 * $Id: assets_controller.php 196 2011-11-09 06:25:46Z manu $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 196 $
 * @lastModifiedBy		$LastChangedBy: manu $
 * @lastChanged			$Date: 2011-11-09 11:55:46 +0530 (Wed, 09 Nov 2011) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Do not use this controlller directly.It can be only be used if extended. Like for Recipe has Many Asset then we should create a controller
 * RecipeAssets and Model RecipeAsset also add Multi upload behaviour to Recipe model.
 *  
 *
 */
class AssetsController extends AppController {

	var $name = 'Assets';
	var $helpers = array('Html', 'Form','Assets.Asset','Time', 'Session','Assets.Gallery');
	//var $paginate = array('limit' => 16, 'page' => 1, 'order'=>array('time'=>'desc'));
	var $abstractView = 'Assets.Assets';//name plugin for including view path
	
	function beforeRender()
	{
		if($this->name != 'Assets')
		{
			$viewPaths = am(Configure::read('viewPaths'), ASSETS_PATH.'views'.DS);
			Configure::write('viewPaths', $viewPaths);
		}
		parent::beforeRender();
	}
	
	function beforeFilter()
	{
		$this->Asset = $this->{$this->modelNames[0]};
		//prd($this->Asset);
		$this->set('assetModelName',$this->Asset->alias);
		parent::beforeFilter();
	}
	//var $helpers = array();
	
	function test()
	{
		
	}
	
	
	/**
	 * Generic add function
	 */
	function add($itemId=null)
	{		
		if(!$itemId)
		{
			$this->redirect($this->referer());
		}
		
		if($this->data)
		{
			$this->data[$this->Asset->alias][$this->Asset->getDependentModel()->foreignKey] =  $itemId;
			if($this->getAuthModel())
				$this->data[$this->Asset->alias][Inflector::underscore($this->getAuthModel()).'_id'] = $this->getAuthModel('id');
			$this->Asset->create();
			//prd($this->data);
			if($this->Asset->save($this->data))
			{
				$this->successMsg(sprintf(__('%s has been added'),$this->name));
				$this->afterAdd();
				$this->redirect("/");
			}else
			{				
				$this->afterAddFail();
			}
		}
	}

	/**
	 * Display a user uploaded asset of a specific $type
	 *
	 * @param unknown_type $type
	 */


	/**
	 * Moderator/admin's edit function
	 *
	 * @param unknown_type $id
	 */
	function edit($id)
	{
		$this->restrictUserType('Moderator','Admin');
		$this->recursive =-1;
		if (!$id) 
		{
			$this->flash(__('Invalid Review', true), '/');
		}

		if(!empty($this->data))
		{
			$this->data['Asset']['id'] = $id;
			if($this->Asset->save($this->data))
			{
				$this->successMsg(__('Photo has been modified.'));
				$this->redirect($this->Session->read('redirectTo'));
			}else
				$this->errorMsg(__('The Review has not been modified.Please try again.'));
		}
		elseif(empty($this->data))
		{
			$this->data = $this->Asset->read(null,$id);
			$this->Session->write('redirectTo',$this->referer());
		}
	}

//this function for admin index and search of specific type asset
	function admin_index1()
	{	
		$hasError = false;
		$condition ='';
		$default = array('type'=>'','item_id'=>'','status'=>'','media_type'=>'');
		if($this->params['named'])
		{
			$params = array_intersect_key($this->params['named'],$default);
		}
		if(empty($params))
			$hasError = true;
		else
		{	$this->Asset->recursive = -1;
			$this->paginate['conditions'] = $params;
			$this->paginate['limit'] = 2;
			$assets = $this->paginate('Asset');
			$this->set('assets',$assets);
			if(empty($assets))
				$hasError =true;
		}
		if($hasError == true)
		{
			$this->errorMsg(__('Invalid Request!'));
			$this->redirect($this->referer());
		}
	}

	function listPhoto($username = null)
	{
		if(empty($username))
		{
			$userInfo = $this->Session->read('Auth.User');
			$userId = $userInfo['id'];
			$username = $userInfo['username'];
		}
		else
		{
			$userId = $this->Asset->User->field('id',array('username'=>$username));
		}
		$this->paginate['conditions'] = array('Asset.user_id'=>$userId,'Asset.status'=>1);
		$assets = $this->paginate();
		$this->set('assets',$assets);
		$this->set('username',$username);
	}

	function admin_index($status=null)
	{
		$statusArr = array('0'=>'unpublished','1'=>'published');
		if(in_array($status,$statusArr))
		{
			$statusArr = array_flip($statusArr);
			$this->paginate['conditions'][] = array('Asset.status'=>$statusArr[$status]);
		}elseif(!empty($status))
		{
			$this->redirect('/admin');
		}
		$this->paginate['limit'] = 10;
		$this->paginate['order'] = array('Asset.time DESC');
		$this->Asset->recursive = 1;
		$this->set('assets',$this->paginate('Asset'));
		$this->set('status',$status);

	}


	function admin_edit($id = null) 
	{
		if (!empty($this->data)) 
		{				
			if ($this->Asset->save($this->data)) 
			{
				$this->successMsg(__('The item has been modified.'));
				$this->redirect($this->Session->read('redirectTo'));
			} 
		}
		
		elseif(empty($this->data)) 
			{
				
				$this->Session->write('redirectTo',$this->referer());
			}

		if (empty($this->data)) 
		{
			$this->Asset->recursive=-1;
			$this->data = $this->Asset->read(null, $id);
		}
	}
	
	function admin_delete($id=null)
	{	
		if (!$id) 
		{
			$this->errorMsg(__('Invalid Request'));
			$this->redirect($this->referer());
		}
		
		if ($this->Asset->del($id)) 
		{
			$this->successMsg(__('Recipe Image deleted.'));
			$this->redirect($this->referer());
		}

	}

	function admin_view($id = null) {

		if (!$id) {
			$this->flash(__('Invalid description', true), array('action'=>'index'));
		}
		App::import('Model','Comments.Comment');
			$commentObj = new Comment();
			$comment = $commentObj->find('all', array('conditions' => array('Comment.type' =>'Asset','Comment.item_id'=>$id)));
		
		$this->set('comments',$comment);
		$data=$this->Asset->read(null,$id);
		$this->set('data',$data);

		App::import('Model','Cafe');
			$commentObj = new Cafe();
			$cafe = $commentObj->find('all', array('conditions' => 'Cafe.id='.$data['Asset']['item_id'],'recursive' => -1));
			
		$this->set('cafeName',$cafe[0]['Cafe']['name']);
	}
	
	function admin_stats()
	{
		Configure::write('debug',0);
		echo __("Too many files found.");
		exit;
	}

	function admin_publish($id=null)
	{
		if(empty($id))
		{
			$this->errorMsg(__('Invalid request'));
			$this->redirect('/admin/assets');
		}
		if($this->Asset->saveField('status',1))
		{
			$this->successMsg(__('Image published'));
			$this->redirect('/admin/assets');
		}
		else
		{
			$this->errorMsg(__('Image could not be published. Try again!'));
			$this->redirect('/admin/assets');
		}
	}

	function admin_unpublish($id=null)
	{
		if(empty($id))
		{
			$this->errorMsg(__('Invalid request'));
			$this->redirect('/admin/assets');
		}
		if($this->Asset->saveField('status',2))
		{
			$this->successMsg(__('Image unpublished'));
			$this->redirect('/admin/assets');
		}
		else
		{
			$this->errorMsg(__('Image could not be unpublished. Try again!'));
			$this->redirect('/admin/assets');
		}
	}
	
	function getDisplayName()
	{
		
	}
	
	function afterAdd(){
		
	}
	
	function afterAddFail(){
		
	}
	
	function delete($id=null,$redirect=array(),$msg=null)
	{
		if(!$id)
			$this->redirect($this->referer());
		if($this->Asset->del($id))
		{
			$msg = is_null($msg) ? __('Item has been deleted') : $msg;
			if(!is_array($redirect))
				$redirect = $this->referer();
			$this->redirect($redirect);
		}else
		{
			$this->errorMsg(__('Unable to delete item please try again'));
		}
		$this->redirect($this->referer());
	}
	
	function index()
	{
		$this->set('assets',$this->paginate());
	}

//enf of the class
}?>